package com.pan.admin.module.channel.controller;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.base.enums.Sort;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.common.utils.poi.ExcelUtil;
import com.pan.admin.module.channel.entity.LoanChannelMarket;
import com.pan.admin.module.channel.service.LoanChannelMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 渠道关联产品Controller
 *
 * @author ts
 * @date 2023-09-18
 */
@RestController
@RequestMapping("/channel/market")
public class LoanChannelMarketController extends BaseController {

    @Autowired
    private LoanChannelMarketService loanChannelMarketService;

    /**
     * 查询渠道关联产品列表
     */
    @PreAuthorize("@ss.hasPermi('channel:market')")
    @GetMapping("/list")
    public TableDataInfo list(LoanChannelMarket loanChannelMarket) {
        startPage();
        //  只查询启用的
        loanChannelMarket.setStatus(1);
        loanChannelMarket.setMarketStatus(1);
        List<LoanChannelMarket> list = loanChannelMarketService.list(loanChannelMarket);

        //  排序展示字段
        if (ValidParam.isNotEmpty(list)) {
            LoanChannelMarket first = IteratorUtils.getFirst(list);
            //  展示模式排序
            Integer marketDisplayMode = first.getMarketDisplayMode();
            if (marketDisplayMode.equals(0)) {
                CollUtils.sortNumber(list, "price", Sort.DESC);
            }
            if (marketDisplayMode.equals(1)) {
                CollUtils.sortNumber(list, "weight", Sort.DESC);
            }
            if (marketDisplayMode.equals(2)) {
                CollUtils.sortNumber(list, "sort", Sort.DESC);
            }

            //  展示头部排序
            LoanChannelMarket top = CollUtils.findOne(list, t -> ValidParam.isNotNull(t.getEnableTop()) && t.getEnableTop().equals(1));
            if (ValidParam.isNotNull(top)) {
                list.remove(top);
                list.addFirst(top);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出渠道关联产品列表
     */
    @PreAuthorize("@ss.hasPermi('channel:market:export')")
    @Log(title = "渠道关联产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LoanChannelMarket loanChannelMarket) {
        List<LoanChannelMarket> list = loanChannelMarketService.list(loanChannelMarket);
        ExcelUtil<LoanChannelMarket> util = new ExcelUtil<LoanChannelMarket>(LoanChannelMarket.class);
        util.exportExcel(response, list, "渠道关联产品数据");
    }

    /**
     * 获取渠道关联产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('channel:market')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanChannelMarketService.getById(id));
    }

    /**
     * 新增渠道关联产品
     */
    @PreAuthorize("@ss.hasPermi('channel:market')")
    @Log(title = "渠道关联产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanChannelMarket loanChannelMarket) {
        return toAjax(loanChannelMarketService.insertLoanChannelMarket(loanChannelMarket));
    }

    /**
     * 修改渠道关联产品
     */
    @PreAuthorize("@ss.hasPermi('channel:market')")
    @Log(title = "渠道关联产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanChannelMarket loanChannelMarket) {
        return toAjax(loanChannelMarketService.updateLoanChannelMarket(loanChannelMarket));
    }

    /**
     * 删除渠道关联产品
     */
    @PreAuthorize("@ss.hasPermi('channel:market')")
    @Log(title = "渠道关联产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(loanChannelMarketService.batchDeleteById(ids));
    }
}
